//点击登录按钮
//(1)收集表单数据
//(2)校验数据：非空+正则
//(3)发送ajax
// (4)响应成功 跳转

//点击登录按钮
document.querySelector('#btn-login').addEventListener('click',async function(){
    //收集表单的值 value(声明变量)
    const username = document.querySelector('[name="username"]').value
    const password = document.querySelector('[name="password"]').value
    //校验数据
    if(username === "" || password == ""){
     return Toast.fail('用户名和密码不能为空')
    }else if(username.length < 2 || username.length > 30){
     return Toast.fail('用户名必须在2-30之间')
    }else if(password.length < 6 || password.length > 30){
     return Toast.fail('密码必须在6-30之间')
    }
    //发送ajax 用解构
    const {data} = await axios.post('/login',{username,password})
    console.log(data);
    //储存token
    localStorage.setItem('token',data.data.token)
    //响应成功 跳转到首页
    Toast.success('登录成功')
    location.href = './index.html'
 })